package com.codebyorder.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codebyorder.entity.Ebook;
import com.codebyorder.mapper.EbookMapper;
import com.codebyorder.req.EbookQueryReq;
import com.codebyorder.resp.EbookQueryResp;
import com.codebyorder.resp.PageResp;
import com.codebyorder.service.EbookService;
import com.codebyorder.util.CopyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 29038
 * @description 针对表【ebook(电子书)】的数据库操作Service实现
 * @createDate 2022-12-05 16:34:31
 */
@Service
public class EbookServiceImpl extends ServiceImpl<EbookMapper, Ebook>
        implements EbookService {
    @Resource
    private EbookMapper ebookMapper;
    private static final Logger LOG = LoggerFactory.getLogger(EbookServiceImpl.class);


    @Override
    public PageResp<EbookQueryResp> ebookToResp(EbookQueryReq ebookQueryReq) {
        PageHelper.startPage(ebookQueryReq.getPage(), ebookQueryReq.getSize());//和第一个查询语句放在一起,请求参数
        List<Ebook> ebooks = ebookMapper.selectByName(ebookQueryReq.getName());

        PageInfo<Ebook> pageInfo = new PageInfo<>(ebooks);
        LOG.info("总行数:{}",pageInfo.getTotal());//返回给前端
        LOG.info("总页数:{}",pageInfo.getPages());

        List<EbookQueryResp> ebookResp = CopyUtil.copyList(ebooks, EbookQueryResp.class);
        PageResp<EbookQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(ebookResp);
        return pageResp;
    }
}